import { createApp } from 'vue'
import App from './App.vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { CanvasRenderer } from 'echarts/renderers'
import { use } from 'echarts/core'
import { BarChart } from 'echarts/charts'
import { GridComponent, TitleComponent, TooltipComponent } from 'echarts/components'
import { useTokenStore } from '@/stores/token'


use([
  CanvasRenderer,
  BarChart,
  GridComponent,
  TitleComponent,
  TooltipComponent
])

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)

// ✅ 创建临时 app 实例来访问 store（还没挂载）
const tempApp = createApp({})
tempApp.use(pinia)
const tokenStore = useTokenStore()

// ✅ 如果未登录且不是在登录页，直接跳转并提示
if (!tokenStore.isLoggedIn) {
  alert('用户未登录，请先登录！')
  window.location.replace('/login.html')
} else {
  if (!tokenStore.isLoggedIn) {
    alert('用户未登录，请先登录！')
    window.location.replace('/login.html')
  } else {
    // ✅ 正常启动应用
    const app = createApp(App)
    app.use(pinia)
    app.use(ElementPlus)

    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
      app.component(key, component)
    }

    app.mount('#app')
  }
}